//
//  ForgetPassWordViewController.m
//  BCNADemo
//
//  Created by 马东朝 on 2018/5/4.
//  Copyright © 2018年 马东朝. All rights reserved.
//

#import "ForgetPassWordViewController.h"

@interface ForgetPassWordViewController ()<SelectBtnsDelegate, LineTextFieldDelegate>
{
    UILabel *resetWayLabel;
    SelectBtns *selectBrn;
    UILabel *backWayLabel;
    UILabel *verificationCodeLabel;
    UILabel *passwordLabel;
    LineTextField *userIDTf;///账号
    LineTextField*verificationCodeTf;///验证码
    LineTextField*passwordTf;///密码
    UIButton *resetPasswordBtn;
    
    NSMutableDictionary *resetDic;
}
@end

@implementation ForgetPassWordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    resetDic = [NSMutableDictionary dictionaryWithCapacity:0];
    [resetDic setValue:[NSString enCryptKey:postKey strPassword:@"2"]  forKey:@"type"];
    // Do any additional setup after loading the view.
    [self addSelectRegistrationBts];
    [self addUserIDSubViews];
    [self addVerificationCodeSubViews];
    [self addPasswordSubViews];
    [self addResetBtnSubViews];
//    [self setFameWithEmail];
}
///注册方式选择按钮
- (void)addSelectRegistrationBts{
    resetWayLabel = [[UILabel alloc] initWithFrame:CGRectMake(kWidth(100), kHeight(235), kWidth(900), kHeight(70))];
    resetWayLabel.text = StringWithKey(@"ForgetVC0");
    resetWayLabel.textColor =  [UIColor whiteColor];
    resetWayLabel.font = [UIFont systemFontOfSize: kHeight(60)];
    selectBrn= [[SelectBtns alloc] initWithFrame:CGRectMake(kWidth(40), kHeight(235), kWidth(900), kHeight(70))];
    selectBrn.delegate = self;
    [selectBrn setBtntitleWithArray:@[StringWithKey(@"ForgetVC1"), StringWithKey(@"ForgetVC0")]];
    [self.view addSubview:resetWayLabel];
}
///账户
- (void)addUserIDSubViews{
    backWayLabel = [[UILabel alloc] initWithFrame:CGRectMake(kWidth(100), CGRectGetMaxY(selectBrn.frame) + kHeight(90),ScreenWidth - kWidth(200), kHeight(60))];
    [self.view addSubview:backWayLabel];
    backWayLabel.text = StringWithKey(@"LgVC6");
    backWayLabel.textColor = [UIColor colorWithHexString:@"#ffffff"];
    backWayLabel.textAlignment = NSTextAlignmentLeft;
    backWayLabel.font = [UIFont systemFontOfSize:kHeight(52)];
    userIDTf = [[LineTextField  alloc] initWithFrame:CGRectMake(kWidth(90), CGRectGetMaxY(backWayLabel.frame) + kHeight(60), ScreenWidth-kWidth(180), kHeight(145))];
    
    [userIDTf setTextConentStyleWith:TextWayIphoneNumber];
    [self.view addSubview:userIDTf];
}
///验证码
- (void)addVerificationCodeSubViews{
    verificationCodeLabel = [[UILabel alloc] initWithFrame:CGRectMake(kWidth(100), CGRectGetMaxY(userIDTf.frame) + kHeight(60),ScreenWidth - kWidth(200), kHeight(52))];
    [self.view addSubview:verificationCodeLabel];
    
    verificationCodeLabel.text = StringWithKey(@"RegistVC7");
    verificationCodeLabel.textColor = [UIColor colorWithHexString:@"#ffffff"];
    verificationCodeLabel.textAlignment = NSTextAlignmentLeft;
    verificationCodeLabel.font = [UIFont systemFontOfSize:kHeight(52)];
    
    verificationCodeTf = [[LineTextField  alloc] initWithFrame:CGRectMake(kWidth(90), CGRectGetMaxY(verificationCodeLabel.frame) + kHeight(60), ScreenWidth-kWidth(180), kHeight(145))];
    verificationCodeTf.delegate = self;
    [verificationCodeTf setTextConentStyleWith:TextWayVerificationCode];
    [self.view addSubview:verificationCodeTf];
    
}
///密码
- (void)addPasswordSubViews{
    passwordLabel = [[UILabel alloc] initWithFrame:CGRectMake(kWidth(100), CGRectGetMaxY(verificationCodeTf.frame) + kHeight(60), ScreenWidth-kWidth(200), kHeight(60))];
    [self.view addSubview:passwordLabel];
    passwordLabel.text = StringWithKey(@"ForgetVC3");
    passwordLabel.textColor = [UIColor colorWithHexString:@"#ffffff"];
    passwordLabel.textAlignment = NSTextAlignmentLeft;
    passwordLabel.font = [UIFont systemFontOfSize:kHeight(52)];
    
    
    passwordTf = [[LineTextField  alloc] initWithFrame:CGRectMake(kWidth(90), CGRectGetMaxY(passwordLabel.frame) + kHeight(60), ScreenWidth-kWidth(180), kHeight(145))];
    
    [passwordTf setTextConentStyleWith:TextWayPassWord];
    [self.view addSubview:passwordTf];
}
///重置按钮已经协议
- (void)addResetBtnSubViews{
    resetPasswordBtn = [self setBtnWithFrame:CGRectMake(kWidth(90), CGRectGetMaxY(passwordTf.frame) + kHeight(75), ScreenWidth-kWidth(180), kHeight(150)) title:StringWithKey(@"ForgetVC2")];
    resetPasswordBtn.backgroundColor = [UIColor colorWithHexString:@"#40b5f8"];
    [resetPasswordBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.view addSubview:resetPasswordBtn];
    [resetPasswordBtn addTarget:self action:@selector(handleReset) forControlEvents:UIControlEventTouchUpInside];
}
- (UIButton *)setBtnWithFrame:(CGRect) frame title:(NSString *)title{
    UIButton *abutton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    abutton.frame = frame;
    [abutton setTitle:title forState:UIControlStateNormal];
    abutton.layer.cornerRadius = frame.size.height/2;
    abutton.layer.masksToBounds = YES;
    
    return abutton;
}
#pragma mark -- SelectBtnsDelegate
- (void)selectLeftBtn{
    [self tfEesignFirstResponder];
    [resetDic setValue:[NSString enCryptKey:postKey strPassword:@"1"] forKey:@"type"];
    backWayLabel.text = StringWithKey(@"Account1");
    [userIDTf setTextConentStyleWith:TextWayEmail];
    passwordTf.textField.text = @"";
    [self setFameWithEmail];
    
}
- (void)setFameWithEmail{
    verificationCodeTf.hidden = YES;
    verificationCodeLabel.hidden = YES;
    passwordLabel.frame =  CGRectMake(kWidth(100), CGRectGetMaxY(userIDTf.frame) + kHeight(60), ScreenWidth - kWidth(200), kHeight(60));
    passwordTf.frame = CGRectMake(kWidth(90), CGRectGetMaxY(passwordLabel.frame) + kHeight(60), ScreenWidth-kWidth(180), kHeight(135));
    resetPasswordBtn.frame = CGRectMake(kWidth(90), CGRectGetMaxY(passwordTf.frame) + kHeight(75), ScreenWidth-kWidth(180), kHeight(150));
//    goLoginBtn.frame = CGRectMake((ScreenWidth - kWidth(440))/2, CGRectGetMaxY(registBtn.frame) + kHeight(35), kWidth(440), kHeight(150));
}
- (void)setFrameWithIphoneNumber{
    
    verificationCodeTf.hidden = NO;
    verificationCodeLabel.hidden = NO;
    passwordLabel.frame =  CGRectMake(kWidth(100), CGRectGetMaxY(verificationCodeTf.frame) + kHeight(60), ScreenWidth - kWidth(200), kHeight(60));
    passwordTf.frame = CGRectMake(kWidth(90), CGRectGetMaxY(passwordLabel.frame) + kHeight(60), ScreenWidth-kWidth(180), kHeight(135));
    resetPasswordBtn.frame = CGRectMake(kWidth(90), CGRectGetMaxY(passwordTf.frame) + kHeight(75), ScreenWidth-kWidth(180), kHeight(150));
//    goLoginBtn.frame = CGRectMake((ScreenWidth - kWidth(440))/2, CGRectGetMaxY(registBtn.frame) + kHeight(35), kWidth(440), kHeight(150));
    
    
}
- (void)tfEesignFirstResponder{
    [userIDTf.textField resignFirstResponder];
    [verificationCodeTf.textField  resignFirstResponder];
    [passwordTf.textField  resignFirstResponder];
}
- (void)selectRightBtn{
    [resetDic setValue:[NSString enCryptKey:postKey strPassword:@"2"] forKey:@"type"];
     [self tfEesignFirstResponder];
    passwordTf.textField.text = @"";
    backWayLabel.text = StringWithKey(@"LgVC6");
    [userIDTf setTextConentStyleWith:TextWayIphoneNumber];
    [self setFrameWithIphoneNumber];
}
#pragma mark -- 按钮点击事件
- (void)handleReset{
    NSLog(@"按钮点击事件");
    NSString *sty = [resetDic valueForKey:@"type"];
    
    [userIDTf resignFirstResponder];
    if (userIDTf.returnRight == YES ) {
        
        if ([sty isEqualToString:[NSString enCryptKey:postKey strPassword:@"1"]]) {
            
//            [resetDic setValue:sty forKey:@"type"];
            [self chectPassword];
        }else{
            
//            [resetDic setValue:sty forKey:@"type"];
            [verificationCodeTf resignFirstResponder];
            if (verificationCodeTf.returnRight == YES) {
                
                [self chectPassword];
            }else{
                return;
            }
        }
    }else{
        return;
    }
    
}
- (void)chectPassword{
    NSString *sty = [resetDic valueForKey:@"type"];
    NSMutableString *urlStr = [NSMutableString stringWithCapacity:0];
    [passwordTf resignFirstResponder];
    if (passwordTf.returnRight == YES) {
        [userIDTf.textField resignFirstResponder];
        ///手机号登录
        if ([sty isEqualToString:[NSString enCryptKey:postKey strPassword:@"1"]]) {
            urlStr  = [NSMutableString stringWithString:@"/password/reset-by-email"];
//            [resetDic setValue:@"" forKey:@"code"];
//            [resetDic setValue:@"" forKey:@"sms_code"];
//            [resetDic setValue:@"" forKey:@"phone"];
            [resetDic setValue:[NSString enCryptKey:postKey strPassword:userIDTf.textField.text] forKey:@"email"];
        }else{
            urlStr  = [NSMutableString stringWithString:@"/password/reset-by-phone"];
            [resetDic setValue:[NSString enCryptKey:postKey strPassword:[[NSUserDefaults standardUserDefaults] valueForKey:CodeStr]] forKey:@"code"];
            [resetDic setValue:[NSString enCryptKey:postKey strPassword:verificationCodeTf.textField.text] forKey:@"sms_code"];
            
            [resetDic setValue:[NSString enCryptKey:postKey strPassword:userIDTf.textField.text] forKey:@"phone"];
        }
        
        
        [resetDic setValue:[NSString enCryptKey:postKey strPassword:passwordTf.textField.text] forKey:@"password"];
        NSLog(@"loginDic%@==%@", resetDic, [NSString stringWithFormat:@"%@%@", UrlAddress, urlStr]);
        
       
        [GLHttpTool post:[NSString stringWithFormat:@"%@%@", UrlAddress, urlStr] params:resetDic success:^(id responseObj) {
            NSLog(@"responseObj==%@", responseObj);
            NSString *code = [NSString stringWithFormat:@"%@", responseObj[@"code"]];
            NSString *msg = [NSString stringWithFormat:@"%@", responseObj[@"msg"]];
            if ([code isEqualToString:@"200"]) {
                
                [self.navigationController popToRootViewControllerAnimated:YES];
            }else{
                
                AlterView(StringWithKey(@"ALter0"), msg, StringWithKey(@"ALter8"));
                
            }
        } failure:^(NSError *error) {
            NSLog(@"error==%@", error);
        }];
    }else{
        return;
    }
}
- (void)handleGetSMSCode{
    NSMutableDictionary *phoneDic= [NSMutableDictionary dictionaryWithCapacity:0];
    if (userIDTf.textField.text.length > 0) {
        [phoneDic setValue:[NSString enCryptKey:postKey strPassword:userIDTf.textField.text] forKey:@"phone"];
        [phoneDic setObject:[NSString enCryptKey:postKey strPassword:[[NSUserDefaults standardUserDefaults] valueForKey:CodeStr]] forKey:@"code"];
        [verificationCodeTf postSMSCodeWithPhoneDic:phoneDic];
    }else{
        AlterView(StringWithKey(@"ALter0"), StringWithKey(@"ALter6"), StringWithKey(@"ALter8"));
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
